package com.ruoyi.web.controller.system;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.service.ISysDeptService;

import com.ruoyi.system.domain.vo.SjExecutionHistoryVO;
import com.ruoyi.system.domain.vo.SjHistoryDoubtDistributionVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SjExecutionHistory;
import com.ruoyi.system.service.ISjExecutionHistoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 历史记录Controller
 * 
 * @author zrr
 * @date 2024-11-04
 */
@RestController
@RequestMapping("/executionHistory/executionHistory")
public class SjExecutionHistoryController extends BaseController
{
    @Autowired
    private ISjExecutionHistoryService sjExecutionHistoryService;
    @Autowired
    private ISysDeptService deptService;

    /**
     * 查询历史记录列表
     */
    @PreAuthorize("@ss.hasPermi('executionHistory:executionHistory:list')")
    @GetMapping("/list")
    public TableDataInfo list(SjExecutionHistory sjExecutionHistory)
    {
        startPage();
        if(sjExecutionHistory.getExecutionTime() != null){
            sjExecutionHistory.setExecutionMonth(DateUtils.parseDateToStr("yyyy-MM",sjExecutionHistory.getExecutionTime())+"%");
        }
        List<SjExecutionHistory> list = sjExecutionHistoryService.selectSjExecutionHistoryList(sjExecutionHistory);
        return getDataTable(list);
    }

    /**
     * 导出历史记录列表
     */
    @PreAuthorize("@ss.hasPermi('executionHistory:executionHistory:export')")
    @Log(title = "历史记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SjExecutionHistory sjExecutionHistory)
    {
        List<SjExecutionHistory> list = sjExecutionHistoryService.selectSjExecutionHistoryList(sjExecutionHistory);
        ExcelUtil<SjExecutionHistory> util = new ExcelUtil<SjExecutionHistory>(SjExecutionHistory.class);
        util.exportExcel(response, list, "历史记录数据");
    }

    /**
     * 获取历史记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('executionHistory:executionHistory:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(sjExecutionHistoryService.selectSjExecutionHistoryById(id));
    }

    /**
     * 新增历史记录
     */
    @PreAuthorize("@ss.hasPermi('executionHistory:executionHistory:add')")
    @Log(title = "历史记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SjExecutionHistory sjExecutionHistory)
    {
        if(!sjExecutionHistory.getDepartmentCode().isEmpty()){
            SysDept sd = deptService.selectDeptById(Long.parseLong(sjExecutionHistory.getDepartmentCode()));
            sjExecutionHistory.setDepartment(sd.getDeptName());
        }
        return toAjax(sjExecutionHistoryService.insertSjExecutionHistory(sjExecutionHistory));
    }

    /**
     * 修改历史记录
     */
    @PreAuthorize("@ss.hasPermi('executionHistory:executionHistory:edit')")
    @Log(title = "历史记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SjExecutionHistory sjExecutionHistory)
    {
        if(!sjExecutionHistory.getDepartmentCode().isEmpty()){
            SysDept sd = deptService.selectDeptById(Long.parseLong(sjExecutionHistory.getDepartmentCode()));
            sjExecutionHistory.setDepartment(sd.getDeptName());
        }
        return toAjax(sjExecutionHistoryService.updateSjExecutionHistory(sjExecutionHistory));
    }

    /**
     * 删除历史记录
     */
    @PreAuthorize("@ss.hasPermi('executionHistory:executionHistory:remove')")
    @Log(title = "历史记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(sjExecutionHistoryService.deleteSjExecutionHistoryByIds(ids));
    }

    /**
     * 执行历史查询
     */
    @PreAuthorize("@ss.hasPermi('executionHistory:executionHistory:count')")
    @GetMapping("/historyCount")
    public SjExecutionHistoryVO selectExecutionHistoryCount(){
        return sjExecutionHistoryService.selectSjExecutionHistoryCount();
    }

    /**
     * 疑点分布查询
     */
    @PreAuthorize("@ss.hasPermi('executionHistory:executionHistory:distribution')")
    @GetMapping("/distribution")
    public List<SjHistoryDoubtDistributionVO> selectDistribution(){
        return sjExecutionHistoryService.selectDistribution();
    }
}
